import wizard
import pooler
from tools.misc import UpdateableStr,UpdateableDict
import time
import operator
from osv import osv, fields
from osv.orm import intersect
import tools.sql
from tools import config
from tools.translate import _
from mx import DateTime



inputform='''<form string="Input Payment number">
    <separator string="Input payment number?"/>
    <field name='paymentno'/>
</form>'''

Fields=UpdateableDict()

def _get_paymentno(self, cr, uid, data, context):
    #if data   
    pool = pooler.get_pool(cr.dbname)
    ids=data['ids']
    prj_obj=pool.get('kdvn.contract.client')
    prj_ids=prj_obj.search(cr,uid,[('id','in',ids)],limit=1)
    paymentno=prj_obj.read(cr,uid,prj_ids,['progressno'])
    if paymentno:
        paymentno = paymentno[0]['progressno']
    else:
        paymentno = 1
    Fields['paymentno']= {'string': 'Payment number', 'type': 'integer','default': lambda *a:paymentno}
        
    return {}

def _update_progressno(self, cr, uid, data, context):
	#if data   
    pool = pooler.get_pool(cr.dbname)
    ids=data['ids']
    prj_obj=pool.get('kdvn.contract.client')
    prj_ids=prj_obj.search(cr,uid,[('id','in',ids)])
    if prj_ids:
        cr.execute('update kdvn_contract_client set progressno=' +str(data['form']['paymentno']) +" where id in (" +",".join(map(str,prj_ids))+")" )
    #prj_obj.write(cr,uid,prj_ids,{'progressno':data['form']['paymentno']})
        cr.commit()
    return data['form']

class wizard_progress(wizard.interface):
    """
    Progress Report
    """
    states = {
        'init': {
            'actions': [_get_paymentno],
            'result': {'type': 'form','arch':inputform,'fields':Fields,'state': [
                    ('update_report', '_Print', 'gtk-ok', True),
                    ('end', '_Cancel','gtk-cancel'),
                    ]
                      }},        
             'update_report': {
                'actions': [_update_progressno],
                'result': {'type':'print', 'report':'kdvn.progress.evaluation.sheet.hidden','state':'end'}}
    }
wizard_progress('open_progress')


class wizard_progress_evaluation_sheet(wizard.interface):

    def _get_defaults(self, cr, uid, data, context={}):
        pool = pooler.get_pool(cr.dbname)
        inv_obj = pool.get('kdvn.payment.from.client')
        ids = []
        check=False
        try:
            inv_ids=data['ids']
            check=True
        except:
            inv_ids=data['id']
        if inv_ids:
            list_invs=inv_obj.browse(cr,uid,inv_ids)
            for x in list_invs:
                if x.contract_id:
                    ids.append(x.contract_id.id)
        if check:
            data['ids']=ids
        else:
            data['id']=ids
        ids=data['ids']
        prj_obj=pool.get('kdvn.contract.client')
        if ids:
            paymentno=prj_obj.read(cr,uid,ids,['progressno'])#[0]['progressno']
            if paymentno:
                paymentno = paymentno[0]['progressno']
            else:
                paymentno = 1
        else:
            paymentno = 0
        Fields['paymentno']= {'string': 'Payment number', 'type': 'integer','default': lambda *a:paymentno}
        return data['form']
        
    def _get_records(self, cr, uid, data, context={}):
        pool = pooler.get_pool(cr.dbname)
        ids=[]
        inv_obj = pool.get('kdvn.payment.from.client')
        ids = []
        check=False
        try:
            inv_ids=data['ids']
            check=True
        except:
            inv_ids=data['id']
        if inv_ids:
            list_invs=inv_obj.browse(cr,uid,inv_ids)
            for x in list_invs:
                if x.contract_id:
                    ids.append(x.contract_id.id)
        #prj_obj=pool.get('kdvn.payment.from.client')
        #prj_ids=prj_obj.search(cr,uid,[('id','in',ids)])
        if ids:
            cr.execute('update kdvn_contract_client set progressno=' +str(data['form']['paymentno']) +" where id in (" +",".join(map(str,ids))+")" )
            cr.commit()
        data['ids']=ids
        data['model']='kdvn.contract.client'
        if ids:
            data['id']=ids[0]
        return {'ids' : ids,'id':data['id'],'model':'kdvn.contract.client', 'title' : 'Progress Evaluation Sheet'} 
        
    states = {
        'init': {
            'actions': [_get_defaults],
            'result': {
                'type': 'form',
                'arch':inputform,
                'fields':Fields,
                'state':[('print_report','_Print', 'gtk-ok', True),
                         ('end','Cancel','gtk-cancel')]
            },
        },
        'print_report': {
            'actions': [_get_records],
            'result': {
                'type': 'print',
                'name' : 'Progress',
                'report': 'kdvn.progress.evaluation.sheet.hidden',
                'rml' : 'account_analytic_summary/report/progress_evaluation_sheet.jrxml',
                'state': 'end'
            },
        },
    }
wizard_progress_evaluation_sheet('jreports.progress_evalutation_sheet.wizard')